﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace LogBase
{
    /// <summary>
    /// 站点配置操作类
    /// </summary>
    public class SiteManage
    {
        /// <summary>
        /// 获取所有站点配置列表
        /// </summary>
        /// <returns>DataTable</returns>
        public static DataTable getAll()
        {
            string sql = "select [GUID],[SiteName],[TelDirectory],[UniDirectory],[Enable],(case [Enable] when 1 then '启用' else '禁用' end) status from [QT_SiteList] order by [SiteName] asc";
            DataTable dt = new QT.DBO.SqlDBHelper(MyDB.ConnString).DataTable(sql);
            return dt;
        }

        /// <summary>
        /// 获取启用状态的站点配置列表
        /// </summary>
        /// <returns>DataTable</returns>
        public static DataTable getUsing()
        {
            string sql = "select [GUID],[SiteName],[TelDirectory],[UniDirectory] from [QT_SiteList] where [enable]=1 order by [SiteName] asc";
            DataTable dt = new QT.DBO.SqlDBHelper(MyDB.ConnString).DataTable(sql);
            return dt;
        }

        /// <summary>
        /// 新增站点配置
        /// </summary>
        /// <param name="sitename">站点名称</param>
        /// <param name="teldirectory">电信日志路径</param>
        /// <param name="unidirectory">联通日志路径</param>
        /// <param name="enable">是否启用</param>
        /// <returns>int：操作数量</returns>
        public static int Add(string sitename, List<string> teldirectory, List<string> unidirectory, string enable)
        {
            if (Exsits(sitename, null)) return -2;

            string sql = "insert into [QT_SiteLIst]([GUID],[SiteName],[TelDirectory],[UniDirectory],[Enable]) values(replace(newid(),'-',''),'" + sitename + "','" + string.Join(",", teldirectory.ToArray()) + "','" + string.Join(",", unidirectory.ToArray()) + "'," + enable + ")";
            int rval = new QT.DBO.SqlDBHelper(MyDB.ConnString).ExecuteNonQuery(sql);
            return rval;
        }

        /// <summary>
        /// 修改站点配置
        /// </summary>
        /// <param name="guid">Guid</param>
        /// <param name="sitename">站点名称</param>
        /// <param name="teldirectory">电信日志路径</param>
        /// <param name="unidirectory">联通日志路径</param>
        /// <param name="enable">是否启用</param>
        /// <returns>int：操作数量</returns>
        public static int Update(string guid, string sitename, List<string> teldirectory, List<string> unidirectory, string enable)
        {
            if (Exsits(sitename, guid)) return -2;

            string sql = "update [QT_SiteLIst] set [SiteName]='" + sitename + "',[TelDirectory]='" + string.Join(",", teldirectory.ToArray()) + "',[UniDirectory]='" + string.Join(",", unidirectory.ToArray()) + "',[Enable]=" + enable + " where [guid]='" + guid + "'";
            int rval = new QT.DBO.SqlDBHelper(MyDB.ConnString).ExecuteNonQuery(sql);
            return rval;
        }

        /// <summary>
        /// 删除站点配置
        /// </summary>
        /// <param name="guids">一组Guid</param>
        /// <returns>int：操作数量</returns>
        public static int Delete(List<string> guids)
        {
            string sql = "delete from [QT_SiteList] where [guid] in('" + string.Join("','", guids.ToArray()) + "')";
            int rval = new QT.DBO.SqlDBHelper(MyDB.ConnString).ExecuteNonQuery(sql);
            return rval;
        }

        /// <summary>
        /// 检查站点名称是否存在
        /// </summary>
        /// <param name="sitename">站点名称</param>
        /// <param name="guid">Guid</param>
        /// <returns>bool</returns>
        public static bool Exsits(string sitename,string guid)
        {
            string sql = "select [guid] from [QT_SiteList] where [SiteName]='" + sitename + "'" + (string.IsNullOrEmpty(guid) ? "" : (" and [guid]<>'" + guid + "'"));
            DataTable dt = new QT.DBO.SqlDBHelper(MyDB.ConnString).DataTable(sql);
            if (dt.Rows.Count == 0) return false;
            else return true;
        }
    }
}
